﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DinhMuc.BUL;
using DinhMuc.Public;

namespace DinhMuc.UI.Frm_Form
{
    public partial class frm_themMoiDongHang : Form
    {
        public frm_themMoiDongHang()
        {
            InitializeComponent();
        }
        tblDonHangBUL dh = new tblDonHangBUL();
        DataTable dtPublic = new DataTable();
        int indexRow = 0;
        private void frm_themMoiDongHang_Load(object sender, EventArgs e)
        {
            indexRow = 0;
            loadToComBoBox_maSP();
            loadToComBoBox_maKH();
            loadToComBoBox_DVT();
            dtPublic.Columns.Add("ID");
            dtPublic.Columns.Add("soDH");
            dtPublic.Columns.Add("maSP");
            dtPublic.Columns.Add("maKH");
            dtPublic.Columns.Add("maTrucIn");
            dtPublic.Columns.Add("ngayLap");
            dtPublic.Columns.Add("nguoiPhuTrach");
            dtPublic.Columns.Add("DVT");
            dtPublic.Columns.Add("slDonHang");
            dtPublic.Columns.Add("tgGiaoHang");
            dtPublic.Columns.Add("khoMang");
            dtPublic.Columns.Add("doDay");
            dtPublic.Columns.Add("ghiChu");
            dtPublic.Columns.Add("ketQua");
          
        }
        private void loadToComBoBox_maSP()
        {
            DataTable dt = dh.LoadToComBoBox();
            gridEX1.DropDowns["comBoBox_maSP"].DataSource = dt;
        }
        private void loadToComBoBox_maKH()
        {
            DataTable dt = dh.LoadToComBoBox_maKH();
            gridEX1.DropDowns["comBoBox_maKH"].DataSource = dt;
        }
        private void loadToComBoBox_DVT()
        {
            DataTable dt = dh.LoadToComBoBox_DVT();
            gridEX1.DropDowns["comBoBox_DVT"].DataSource = dt;
        }

        private void gridEX1_RecordAdded(object sender, EventArgs e)
        {
            indexRow++;
            DataRow row = dtPublic.NewRow();
            row["ID"] = indexRow;
            row["soDH"] = gridEX1.GetValue("soDH").ToString();
            row["maSP"] = gridEX1.GetValue("maSP").ToString();
            row["maKH"] = gridEX1.GetValue("maKH").ToString();
            row["maTrucIn"] = gridEX1.GetValue("maTrucIn").ToString();
            row["ngayLap"] = gridEX1.GetValue("ngayLap").ToString();
            row["nguoiPhuTrach"] = gridEX1.GetValue("nguoiPhuTrach").ToString();
            row["DVT"] = gridEX1.GetValue("DVT").ToString();
            row["slDonHang"] = gridEX1.GetValue("slDonHang").ToString();
            row["tgGiaoHang"] = gridEX1.GetValue("tgGiaoHang").ToString();
            row["khoMang"] = gridEX1.GetValue("khoMang").ToString();
            row["doDay"] = gridEX1.GetValue("doDay").ToString();
            row["ghiChu"] = gridEX1.GetValue("ghiChu").ToString();
            //row["ketQua"] = gridEX1.GetValue("ketQua").ToString();
            dtPublic.Rows.Add(row);
            

            //int soDH = int.Parse(gridEX1.GetValue("soDH").ToString());
            //string maSP = gridEX1.GetValue("maSP").ToString();
            //string maTrucIn = gridEX1.GetValue("maTrucIn").ToString();
            //DateTime ngayLap = Convert.ToDateTime(gridEX1.GetValue("ngayLap").ToString());
            //string nguoiPhuTrach = gridEX1.GetValue("nguoiPhuTrach").ToString();
            //string DVT = gridEX1.GetValue("DVT").ToString();
            //int slDonHang = int.Parse("0" + gridEX1.GetValue("slDonHang").ToString());
            //DateTime tgGiaoHang = Convert.ToDateTime(gridEX1.GetValue("tgGiaoHang").ToString());
            //int khoMang = int.Parse("0" + gridEX1.GetValue("khoMang").ToString());
            //float doDay = float.Parse("0" + gridEX1.GetValue("doDay").ToString());
            //string ghiChu = gridEX1.GetValue("ghiChu").ToString();
            //string maKH = gridEX1.GetValue("maKH").ToString();
            //string ketQua = gridEX1.GetValue("ketQua").ToString();

            //tblDonHangPuclic p = new tblDonHangPuclic(
            //      soDH, maSP, maKH, maTrucIn, ngayLap, nguoiPhuTrach, DVT, slDonHang, tgGiaoHang, khoMang, doDay,
            //      ghiChu, ketQua
            //        );
            //dh.ThemDH(p);
            //gridEX1.MoveToNewRecord();
            //frm_themMoiDongHang_Load(sender, e);
        }
        private void insert()
        {
            for (int i = 0; i < dtPublic.Rows.Count; i++)
            {
                
                DataRow row = dtPublic.Rows[i];
                tblDonHangPuclic p = new tblDonHangPuclic();
                p.soDH = int.Parse(row["soDH"].ToString());
                p.maSP = row["maSP"].ToString();
                p.maKH = row["maKH"].ToString();
                p.maTrucIn = row["maTrucIn"].ToString();
                p.ngayLap = Convert.ToDateTime(row["ngayLap"].ToString());
                p.nguoiPhuTrach = row["nguoiPhuTrach"].ToString();
                p.DVT = row["DVT"].ToString();
                p.slDonHang = int.Parse("0" + row["slDonHang"].ToString());
                p.tgGiaoHang = Convert.ToDateTime(row["tgGiaoHang"].ToString());
                p.khoMang = int.Parse("0" + row["khoMang"].ToString());
                p.DoDay = float.Parse("0" + row["doDay"].ToString());
                p.ghiChu = row["ghiChu"].ToString();
                //p.ketQua = row["ketQua"].ToString();


                dh.ThemDH(p);
                gridEX1.MoveToNewRecord();
                //TKSX_NVLADD_Load(sender, e);

            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            insert();
            MessageBox.Show("Thêm thành công");
            this.Close();
        }

        private void gridEX1_RecordUpdated(object sender, EventArgs e)
        {
            DataRow row = dtPublic.Rows[gridEX1.CurrentRow.RowIndex];

            row["soDH"] = gridEX1.GetValue("soDH").ToString();
            row["maSP"] = gridEX1.GetValue("maSP").ToString();
            row["maKH"] = gridEX1.GetValue("maKH").ToString();
            row["maTrucIn"] = gridEX1.GetValue("maTrucIn").ToString();
            row["ngayLap"] = gridEX1.GetValue("ngayLap").ToString();
            row["nguoiPhuTrach"] = gridEX1.GetValue("nguoiPhuTrach").ToString();
            row["DVT"] = gridEX1.GetValue("DVT").ToString();
            row["slDonHang"] = gridEX1.GetValue("slDonHang").ToString();
            row["tgGiaoHang"] = gridEX1.GetValue("tgGiaoHang").ToString();
            row["khoMang"] = gridEX1.GetValue("khoMang").ToString();
            row["doDay"] = gridEX1.GetValue("doDay").ToString();
            row["ghiChu"] = gridEX1.GetValue("ghiChu").ToString();
            //row["ketQua"] = gridEX1.GetValue("ketQua").ToString();

            dtPublic.AcceptChanges();
            gridEX1.DataSource = dtPublic;
        }
       
    }
}
